/**
 * Created by Administrator on 2016/11/15.
 */
function showPic(whichpic) {
    var placeholder = document.getElementById('placeholder');
    if (!placeholder) {
        return true;
    }
    var source = whichpic.getAttribute('href');
    if (placeholder.nodeName != 'IMG') {
        return false;
    }
    placeholder.setAttribute('src', source);
    var title = whichpic.getAttribute('title');
    if (!title) {
        title = '';
    }
    var description = document.getElementById('description');
    if (description) {
        description.firstChild.nodeValue = title;
    }
    return false;
}

function insertAfter(newElement, targetElement) {
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    } else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

function preparePlaceholder() {
    var supported =
        document.getElementsByTagName &&
        document.getElementById &&
        document.createElement &&
        document.createTextNode;
    if (!supported) {
        return false;
    }
    var gallery = document.getElementById('image-gallery');
    if (!gallery) {
        return false;
    }
    var placeholder = document.createElement('img');
    placeholder.setAttribute('id', 'placeholder');
    placeholder.setAttribute('src', 'images/placeholder.gif');
    placeholder.setAttribute('alt', 'my image gallery');
    var description = document.createElement('p');
    description.setAttribute('id', 'description');
    var text = document.createTextNode('Choose an image');
    description.appendChild(text);
    insertAfter(placeholder, gallery);
    insertAfter(description, placeholder);
}

function prepareGallery() {
    var supported =
        document.getElementsByTagName &&
        document.getElementById &&
        document.createElement &&
        document.createTextNode;
    if (!supported) {
        return false;
    }

    var gallery = document.getElementById('image-gallery');
    if (!gallery) {
        return false;
    }

    var links = gallery.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = function () {
            return showPic(this);
        };
    }
}

function addLoadEvent(fn) {
    var oldOnload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = fn;
    } else {
        window.onload = function () {
            oldOnload();
            fn();
        }
    }
}

addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);